package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qhzw.vip.qiyu.entity.RepairMaint;
import net.qhzw.vip.qiyu.mapper.RepairMaintMapper;
import net.qhzw.vip.qiyu.model.CarRentalRepairMaint;
import net.qhzw.vip.qiyu.model.CarRetreatMaintVo;
import net.qhzw.vip.qiyu.service.RepairMaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import net.qhzw.vip.qiyu.util.SecurityUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RepairMaintServiceImpl extends ServiceImpl<RepairMaintMapper, RepairMaint> implements RepairMaintService {

    @Autowired
    private RepairMaintMapper repairMaintMapper;


    @Override
    public Page<RepairMaint> findByPage(Page<RepairMaint> page, RepairMaint repairMaint, Long shopId) {
        page.addOrder(OrderItem.asc("price"));
        return page(page, new LambdaQueryWrapper<RepairMaint>()
                .eq(shopId != 1, RepairMaint::getShopId, shopId)
                .like(repairMaint != null && !StringUtils.isEmpty(repairMaint.getName()),
                        RepairMaint::getName,
                        repairMaint.getName())
                .eq(repairMaint != null && repairMaint.getStatus() != null,
                        RepairMaint::getStatus,
                        repairMaint.getStatus())
        );
    }

    @Override
    public boolean addRepairMaint(RepairMaint repairMaint) {
        repairMaint.setId(IdWorker.getId());
        repairMaint.setStatus((byte) 1);
        repairMaint.setCreateBy(SecurityUtils.getUserId());
        repairMaint.setCreated(new Date());
        return save(repairMaint);
    }

    @Override
    public boolean updateRepairMaint(RepairMaint repairMaint) {
        repairMaint.setCreateBy(SecurityUtils.getUserId());
        repairMaint.setCreated(new Date());
        return updateById(repairMaint);
    }

    /***
     * 根据退车申请表id查询 配件价格 名称
     * @param idList
     * @return
     */
    @Override
    public List<CarRetreatMaintVo> findByRetreatIds(List<Long> idList) {
        return repairMaintMapper.findByRetreatIds(idList);
    }


    /***
     * 根据退车申请表ID查询配件价格
     * @param idList
     * @return
     */
    @Override
    public List<RepairMaint> getRepairMaintByRetreatId(List<Long> idList) {
        return repairMaintMapper.getRepairMaintByRetreatId(idList);
    }


    /***
     * 根据换车申请id查询配件列表
     * @param id
     * @return
     */
    @Override
    public List<RepairMaint> listByTransferId(Long id) {
        return repairMaintMapper.listByTransferId(id);
    }

    /***
     * 根据租车ID查询维修费
     * @param list
     * @return
     */
    @Override
    public List<CarRentalRepairMaint> listByCarRetreatIds(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<CarRentalRepairMaint>();
        }
        return repairMaintMapper.listByCarRetreatIds(list);
    }

    /**
     * 根据退车id查询维修费
     *
     * @param id
     * @return
     */
    @Override
    public BigDecimal getPriceByRetreatId(Long id) {
        return repairMaintMapper.getPriceByRetreatId(id);
    }
}
